package com.first.nell.components.text

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentManager
import com.first.nell.ProjectConfig
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.databinding.TextFirstNellPrivacyPolicyLinkBinding
import com.first.nell.extend.OnFirstNellTextFirstNellViewFirstNellLinkFirstNellClickFirstNellListener
import com.first.nell.extend.addFirstNellLinkFirstNellClickFirstNellModeFirstNellOnFirstNellWords
import com.first.nell.extend.recordFirstNellClickFirstNellEvent
import com.first.nell.extend.startFirstNellSystemFirstNellBrowser
import com.first.nell.utils.UsageCountryUtil

/**
<declare-styleable name="FirstNellPrivacyPolicyText">
<attr name="cwpptText" format="string" />
<attr name="cwpptLinkText" format="string" />
<attr name="cwpptIsChecked" format="boolean" />
<attr name="cwpptIsImgHidden" format="boolean" />
</declare-styleable>
 * */
class PrivacyPolicyLinkText : RelativeLayout, BaseFormAction {

    private lateinit var mFirstNellViewFirstNellBinding: TextFirstNellPrivacyPolicyLinkBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var cwpptText: String? = null
    private var cwpptLinkText: String? = null
    private var cwpptIsChecked: Boolean = true
    private var cwpptIsImgHidden: Boolean = false

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {

        attrs?.let {
            context?.let {
                it.obtainStyledAttributes(attrs, R.styleable.PrivacyPolicyLinkText).apply {
                    cwpptText = getString(R.styleable.PrivacyPolicyLinkText_cwpptText)
                    cwpptLinkText = getString(R.styleable.PrivacyPolicyLinkText_cwpptLinkText)
                    cwpptIsChecked =
                        getBoolean(R.styleable.PrivacyPolicyLinkText_cwpptIsChecked, true)
                    cwpptIsImgHidden =
                        getBoolean(R.styleable.PrivacyPolicyLinkText_cwpptIsImgHidden, false)
                    recycle()
                }
            }
        }

        context?.let {
            vFirstNellRootFirstNellView =
                View.inflate(it, R.layout.text_first_nell_privacy_policy_link, this)
            mFirstNellViewFirstNellBinding =
                TextFirstNellPrivacyPolicyLinkBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textPrivacyPolicyLinkTitle.text = cwpptText

            cwpptLinkText?.let {
                //mViewBinding.textPrivacyPolicyLinkTitle.addUnderLineStyle(it)
                mFirstNellViewFirstNellBinding.textPrivacyPolicyLinkTitle.addFirstNellLinkFirstNellClickFirstNellModeFirstNellOnFirstNellWords(
                    arrayOf(it),
                    object :
                        OnFirstNellTextFirstNellViewFirstNellLinkFirstNellClickFirstNellListener {
                        override fun onClick(word: String, index: Int) {

                            mFirstNellViewFirstNellBinding.textPrivacyPolicyLinkTitle.recordFirstNellClickFirstNellEvent()

                            if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_FCFA)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellGhana()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_GHA)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellKenya()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_KSH)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellUganda()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_UGX)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellNigeria()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_NAIRA)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellVietnam()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_VND)

                            } else if (UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_XAF)
                            } else {
                                context.startFirstNellSystemFirstNellBrowser(ProjectConfig.NARSCASH_Privacy_Policy_Link_UGX)
                            }
                        }
                    })
            }

            mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.setOnClickListener {
                cwpptIsChecked = !cwpptIsChecked
                setFirstNellChecked(cwpptIsChecked)

                mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.recordFirstNellClickFirstNellEvent()
            }

            if (cwpptIsImgHidden) {
                mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.visibility = GONE
                cwpptIsChecked = true
            } else {
                mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.visibility = VISIBLE
            }

            setFirstNellChecked(cwpptIsChecked)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun isFirstNellChecked(): Boolean {
        return cwpptIsChecked
    }

    fun setFirstNellChecked(checked: Boolean) {
        cwpptIsChecked = checked
        if (cwpptIsChecked) {
            mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.setImageResource(R.drawable.first_nell_checkbox_yes_purple)
        } else {
            mFirstNellViewFirstNellBinding.imagePrivacyPolicyLink.setImageResource(R.drawable.first_nell_checkbox_no_purple)
        }
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {

    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {

    }

    override fun setFirstNellTitle(title: String) {

    }

    override fun setFirstNellDetail(detail: String) {

    }

    override fun setFirstNellContent(content: String) {

    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {

    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {

    }
}